package com.mayuhao.adminmanage.service.impl;

import cn.hutool.core.io.IoUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mayuhao.adminmanage.entity.CssAdmin;
import com.mayuhao.adminmanage.mapper.CssAdminMapper;
import com.mayuhao.adminmanage.service.ICssAdminService;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 马宇豪
 * @since 2022-11-14
 */
@Service
public class CssAdminServiceImpl extends ServiceImpl<CssAdminMapper, CssAdmin> implements ICssAdminService {
    /**
     * @return com.baomidou.mybatisplus.extension.plugins.pagination.Page<com.mayuhao.adminmanage.entity.CssAdmin>
     * @description 请修改方法描述
     * @methodName findPage
     * @params [pageNum, pageSize, searchId, searchName]
     * @paramType [java.lang.Integer, java.lang.Integer, java.lang.String, java.lang.String]
     * @author 马宇豪
     * @date 2022/11/14 0014 下午 1:25
     * @email 1757442521@qq.com
     **/
    @Override
    public Page<CssAdmin> findPage(Integer pageNum, Integer pageSize, String searchId, String searchName) {

        LambdaQueryWrapper<CssAdmin> wrapper = Wrappers.lambdaQuery();
        if (!"".equals(searchId)) {
            wrapper.like(CssAdmin::getAdminId, searchId);
        }
        if (!"".equals(searchName)) {
            wrapper.like(CssAdmin::getAdminName, searchName);
        }
        Page<CssAdmin> page = this.page(new Page<>(pageNum, pageSize), wrapper);
        return page;
    }


    @Override
    public void export(HttpServletResponse response) throws UnsupportedEncodingException {
        List<CssAdmin> list = this.list();

        // 通过工具类创建writer
        ExcelWriter writer = ExcelUtil.getWriter(true);

        // 合并单元格后的标题行，使用默认标题样式
        //        writer.merge(1, "学院信息表");

        //自定义标题别名
        //        writer.addHeaderAlias("departmentId", "部门编号");
//        writer.addHeaderAlias("departmentName", "departmentName");
//        writer.addHeaderAlias("professionName", "professionName");

        // 默认的，未添加alias的属性也会写出，如果想只写出加了别名的字段，可以调用此方法排除之
//        writer.setOnlyAlias(true);

        // 一次性写出内容，使用默认样式，强制输出标题
        writer.write(list, true);

        //response为HttpServletResponse对象
        response.setContentType("application/vnd.ms-excel;charset=utf-8");

        //test.xls是弹出下载对话框的文件名，不能为中文，中文请自行编码
        String fileName = URLEncoder.encode("管理员信息表", String.valueOf(StandardCharsets.UTF_8));
        response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx");
        ServletOutputStream out = null;
        try {
            out = response.getOutputStream();
        } catch (IOException e) {
            throw new RuntimeException("输出excel异常");
        }


        writer.flush(out, true);
        // 关闭writer，释放内存
        writer.close();
        //此处记得关闭输出Servlet流
        IoUtil.close(out);
    }

    @Override
    public void imp(MultipartFile file) {
        // 通过工具类创建writer

        InputStream inputStream = null;
        try {
            inputStream = file.getInputStream();
        } catch (IOException e) {
            throw new RuntimeException("导入运行错误");
        }
        ExcelReader reader = ExcelUtil.getReader(inputStream);
        List<CssAdmin> list = reader.readAll(CssAdmin.class);
        this.saveBatch(list);
    }
}
